package com.crm.business.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.crm.business.admin.mapper.AdminAddressBookMapper;
import com.crm.business.admin.service.*;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.common.admin.entity.BO.*;
import com.crm.common.admin.entity.PO.*;
import com.crm.common.admin.entity.VO.AdminUserVO;
import com.crm.common.constant.Constants;
import com.crm.common.core.redis.RedisCache;
import com.crm.common.enums.ErrorCodeConstants;
import com.crm.common.exception.CustomException;
import com.crm.common.ka.core.entity.BasePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-04-27
 */
@Service
@Slf4j
public class AdminAddressBookServiceImpl extends BaseServiceImpl<AdminAddressBookMapper, AdminAddressBook> implements IAdminAddressBookService {

    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询企业下所有用户
     *
     * @param addressBookBO 业务对象
     * @return ids
     */
    @Override
    public BasePage<AdminAddressBook> queryUserList(AdminAddressBookBO addressBookBO) {
        return getBaseMapper().queryUserPage(addressBookBO.parse(), addressBookBO);
    }

    // 通讯录同步
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void synchronousAddressBook(){
        String key = Constants.ADDRESS_BOOK_SYNCHRONOUS;
        String value = UUID.randomUUID().toString();
        // 加锁
        boolean b = redisCache.lock(key, value, 10, TimeUnit.SECONDS);
        if (!b) {
            throw new CustomException(ErrorCodeConstants.LOCK_DATA.getMsg(), ErrorCodeConstants.LOCK_DATA.getCode());
        }

        try {
            List<AdminUserVO> adminUserList = adminUserService.queryBaseUserVoList(null);
            List<AdminAddressBook> adminAddressBookList = new ArrayList<>();
            for (AdminUserVO adminUserVO : adminUserList){
                AdminAddressBook addressBook = getBaseMapper().queryAddressBookByUserId(adminUserVO.getUserId());
                if(ObjectUtil.isEmpty(addressBook)){
                    addressBook = new AdminAddressBook();
                    BeanUtils.copyProperties(adminUserVO, addressBook);
                    addressBook.setCreateTime(new Date());
                    addressBook.setUpdateTime(new Date());
                    save(addressBook);
                }
                adminAddressBookList.add(addressBook);
            }
//        saveBatch(adminAddressBookList);
            System.out.println(JSONObject.toJSONString(adminAddressBookList));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统异常" + e);
        } finally {
            redisCache.unlock(key, value);
        }
    }

    @Override
    public AdminAddressBook queryAddressBookByUserId(Long userId){
        return getBaseMapper().queryAddressBookByUserId(userId);
    }

}
